/**
 * dwz扩展js
 * 
 * @author lidapeng
 */
var batchjobAjaxDone = function(json) {
	DWZ.ajaxDone(json);
	if (json[DWZ.keys.statusCode] == DWZ.statusCode.ok) {
		if (arguments[arguments.length - 1] !== false) {
			// 重新载入当前navTab页面
			var $pagerForm = $("#pagerForm", navTab.getCurrentPanel());
			var args = $pagerForm.size() > 0 ? $pagerForm.serializeArray() : {}
			navTabPageBreak(args, json.rel);
		}
		if ("closeCurrent" == json.callbackType) {
			$.pdialog.closeCurrent();
		}
		DWZ.showDetailedReports(json);
	}
};
(function($, win) {
	// JavaScript中使用国际化资源方法
	win.getLocalRecource = function(keyStr, defaultValue) {
		localRecources = win.localRecources || {};
		if (keyStr) {
			var keys = keyStr.split(',');
			var text = '';
			$.each(keys, function(index, key) {
				text += (localRecources[key] || defaultValue || ('"' + key + '" does not define!'));
			});
			if (arguments.length > 1) {
				for (var index = 1; index < arguments.length; index++) {
					text = text.replace('{' + (index - 1) + '}', arguments[index]);
				}
			}
			return text;
		} else {
			return "Invalid key.";
		}
	}
	/**
	 * @op {type:'',url:'',data:'',callback:''}
	 *     type为get或post；data为传的参数；url参数为全网络路径 ；callback
	 *     返回200才执行的回调函数,传入的参数为服务器端的data值，而非整个返回结果。
	 * 
	 */
	$.DWZajax = function(op) {
		var $callback = op.callback || navTabAjaxDone;
		// 新增错误提示回调函数
		var $errorCallback = op.errorCallback || null;
		if (!$.isFunction($callback))
			$callback = eval('(' + op.callback + ')');
		$.ajax({
			type: op.type || 'POST',
			url: op.url,
			dataType: "json",
			data: op.data,
			async: op.async != undefined ? op.async : true,
			global: op.global != undefined ? op.global : true,
			cache: op.cache != undefined ? op.cache : false,
			success: function(json) {
				if (json.statusCode == DWZ.statusCode.ok) {
					if ($callback == navTabAjaxDone || $callback == dialogAjaxDone) {
						$callback(json);
					} else {
						$callback(json.data);
					}
				} else if (json.statusCode == DWZ.statusCode.error) {
					if (json.message && alertMsg) {
						if ($errorCallback != null && $.isFunction($callback)) {
							$errorCallback(json);
						} else {
							alertMsg.error(json.message);
						}
					}

				} else if (json.statusCode == DWZ.statusCode.timeout) {
					if (alertMsg)
						alertMsg.error(json.message || DWZ.msg("sessionTimout"), {
							okCall: DWZ.loadLogin
						});
					else
						DWZ.loadLogin();
				} else {
					if (json.message && alertMsg)
						alertMsg.correct(json.message);
				}
			},
			error: DWZ.ajaxError
		});
	}
	/**
	 * 滑动面板初始化
	 * 
	 * 
	 * @param contextUrl 相对hostUrl的路径
	 */
	$.fn.initAccordion = function(contextUrl) {
		return this.each(function() {
			var $this = $(this);
			$.DWZajax({
				url: hostUrl + contextUrl,
				cache: true,
				global: false,
				callback: function(data) {
					var html = '<div class="accordion" fillSpace="' + $this.attr("id") + '">' + listAccordion(data) + '</div>';
					$this.find(".accordion").remove().end().append(html);
					initUI($this);
				}
			});
		});
	}
	function listAccordion(dataObj) {
		var content = '';
		if (dataObj == undefined || dataObj == null || dataObj.length == 0) {
			return content;
		}
		$.each(dataObj, function(idx, item) {
			var icon = '', url = '', name = '';
			if (item.icon != undefined && item.icon != null && item.icon != '') {
				icon = style = 'background: url(' + imgHostUrl + item.icon + ');';
			}
			if (item.url != undefined && item.url != null) {
				url = " href=" + hostUrl + item.url + " ";
			} else {
				url = ' href="javascript:void(0);" ';
			}
			if (item.name != undefined && item.name != null) {
				name = item.name;
			}

			if ((item.pid) == undefined || item.pid == null || item.pid == 0) {

				content += '<div class="accordionHeader">' + '<h2><span ' + icon + '>Folder</span>' + name + '</h2>' + '</div>'
				+ '<div class="accordionContent" >';
				content += '<ul class="tree treeFolder">';
				content += listTree(item.trees);
				content += '</ul>';
				content += '</div>';
			}
		});
		return content;
	}
	$.fn.initTree = function(contextUrl, callback) {
		return this.each(function() {
			var $this = $(this);
			$.DWZajax({
				url: hostUrl + contextUrl,
				callback: function(data) {
					var html = '<ul class="tree treeFolder">' + listTree(data) + '</ul>';
					$this.find(".tree").remove().end().append(html);
					initUI($this);
					// $this.find(".accordion").remove().end().append(html).initUI();
					if ($.isFunction(callback)) {
						callback();
					}

				}
			});
		});
	}
	function listTree(dataObj) {
		var content = '';
		if (dataObj == undefined || dataObj == null || dataObj.length == 0) {
			return content;
		}
		$.each(dataObj, function(idx, item) {
			var icon = '', url = '', name = '';
			if (item.icon != undefined && item.icon != null && item.icon != '') {
				icon = style = 'background: url(' + imgHostUrl + item.icon + ');';
			}
			if (item.url != undefined && item.url != null) {
				tempurl = hostUrl + item.url;
				if (tempurl.indexOf("?") >= 0) {// 存在?
					tempurl += "&navTabId=" + item.nodeId;
				} else {// 不存在?
					tempurl += "?navTabId=" + item.nodeId;
				}
				url = ' href="' + tempurl + '" refresh="false" ';
			} else {
				url = ' href="javascript:void(0);"  refresh="false" ';
			}
			if (item.name != undefined && item.name != null) {
				name = item.name;
			}

			if ((item.trees) == undefined || item.trees == null || item.trees.length == 0) {
				content += '<li><a ' + url + ' target="navTab" rel="' + item.nodeId + '">' + name + '</a></li>';
			} else {
				content += '<li><a ' + url + '>' + name + '</a><ul>';
				content += listTree(item.trees);
				content += '</ul></li>';
			}
		});
		return content;
	}
	/**
	 * 语言选择弹框选择
	 * 
	 * @param Callback 会传入当前点击的li标签$selector 回调函数
	 */
	$.fn.switchEnvCallback = function(Callback) {
		var op = {
			cities$: ">ul>li",
			boxTitle$: ">a>span"
		};
		return this.each(function() {
			var $this = $(this);
			$this.click(function() {
				if ($this.hasClass("selected")) {
					_hide($this);
				} else {
					_show($this);
				}
				return false;
			});

			$this.find(op.cities$).click(function() {
				var $li = $(this);
				_hide($this);
				$this.find(op.boxTitle$).html($li.find(">a").html());
				navTab.closeAllTab();
				Callback($li);
				return false;
			});
		});
	}

	function _show($box) {
		$box.addClass("selected");
		$(document).bind("click", {
			box: $box
		}, _handler);
	}
	function _hide($box) {
		$box.removeClass("selected");
		$(document).unbind("click", _handler);
	}

	function _handler(event) {
		_hide(event.data.box);
	}

	DWZ.showDetailedReports = function(json) {
		if (json.data) {
			var data;
			if (typeof json.data == 'object') {
				data = json.data;
			} else {
				data = JSON.parse(json.data);
			}
			var title = data.title;
			if (data.data && data.data.length) {
				$.pdialog.open('', "detailedReport", title, {
					width: 500,
					height: 250,
					minable: false,
					mask: true,
					resizable: false,
					callback: function($content, $dialog) {
						var $grid = $('<div/>').appendTo($content);
						$grid.jGrid({
							layoutH: 30,
							width: $dialog.width() - 14,
							autoLoad: false,
							pageable: false,
							orderable: false,
							columns: [{
								field: 'message',
								text: getLocalRecource('message.C', '消息'),
								width: 150
							}, {
								field: 'result',
								text: getLocalRecource('result.C', '结果'),
								width: 100
							}, {
								field: 'cause',
								text: getLocalRecource('cause.C', '原因')
							}],
							fillData: data.data
						});
					}
				});
				return true;
			}
		}
		return false;
	}
})(jQuery, window);
